package barney.davis.hume.it3048todo.service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;

import barney.davis.hume.it3048todo.constants.TaskStatus;
import barney.davis.hume.it3048todo.dao.ITaskDAO;
import barney.davis.hume.it3048todo.dao.TaskDAOStub;
import barney.davis.hume.it3048todo.dto.Task;

public class TaskServiceStub implements ITaskService {

	/**
	 * {@inheritDoc}
	 * 
	 */
	@Override
	public void saveTask(Task task) throws Exception {
		ITaskDAO taskDAO = new TaskDAOStub();
		taskDAO.save(task);
	}

	/**
	 * {@inheritDoc}
	 * 
	 */
	@Override
	public ArrayList<Task> getTasks() throws Exception {
		ITaskDAO taskDAO = new TaskDAOStub();
		ArrayList<Task> taskList = new ArrayList<Task>();
		taskList = taskDAO.fetchAll();
		return taskList;
	}

	/**
	 * {@inheritDoc}
	 * 
	 */
	@Override
	public ArrayList<Task> getTasks(Calendar dueDate) throws Exception {
		ITaskDAO taskDAO = new TaskDAOStub();
		ArrayList<Task> taskList = new ArrayList<Task>();
		taskList = taskDAO.fetchByDueDate(dueDate, dueDate);
		return taskList;
	}

	/**
	 * {@inheritDoc} Calls TaskDAOStub.getTasks(Calendar) if the supplied dates
	 * surround the hardcoded taskA and taskB within TaskDAOStub.
	 * 
	 * @throws IllegalArgumentException
	 *             when start date is not before end date.
	 */
	@Override
	public ArrayList<Task> getTasks(Calendar startDate, Calendar endDate)
			throws Exception {

		if (startDate.getTimeInMillis() > endDate.getTimeInMillis()) {
			throw new IllegalArgumentException(
					"Start date must be before end date.");
		}

		ITaskDAO taskDAO = new TaskDAOStub();
		ArrayList<Task> taskList = new ArrayList<Task>();

		// Hardcoding some dates that match the hardcoded taskList in TaskDAOStub.
		Calendar dateA = new GregorianCalendar(2013, 11, 10, 17, 15);
		Calendar dateB = new GregorianCalendar(2013, 6, 2, 22, 0);

		// Since the stub implementation TaskDAOStub only has two possible taskList
		// that it can return, we first test to see if taskA is within the
		// supplied dates.
		if ((startDate.getTimeInMillis() <= dateA.getTimeInMillis())
				&& endDate.getTimeInMillis() >= dateA.getTimeInMillis()) {

			taskList.addAll(taskDAO.fetchByDueDate(dateA, dateA));
		}

		// do the same for taskB
		if ((startDate.getTimeInMillis() <= dateB.getTimeInMillis())
				&& endDate.getTimeInMillis() >= dateB.getTimeInMillis()) {
			taskList.addAll(taskDAO.fetchByDueDate(dateB, dateB));
		}

		return taskList;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * Calls TaskDAOStub.fetchByStatus(status)
	 */
	@Override
	public ArrayList<Task> getTasks(TaskStatus status) throws Exception {
		ITaskDAO taskDAO = new TaskDAOStub();
		ArrayList<Task> taskList = new ArrayList<Task>();
		taskList = taskDAO.fetchByStatus(status);
		return taskList;
	}

	/**
	 * {@inheritDoc}
	 * Simply calls TaskDAOStub.delete(task)
	 */
	@Override
	public void deleteTask(Task task) throws Exception {
		ITaskDAO taskDAO = new TaskDAOStub();
		taskDAO.delete(task);

	}

	@Override
	public Task getTaskById(String id) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
